package Year22_Month04.day28;

public class 颠倒字符串中的单词 {
    public static String reverseWords(String s) {
        String[] arr = s.split(" ");
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            String tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
        StringBuilder myString  = new StringBuilder();
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            if(i == len - 1){
               myString.append(arr[i]);
            }else{
                myString.append(arr[i] + " ");
            }
        }
        return myString.toString();
    }

    public static void main(String[] args) {
        String s = "  hello world  ";
        System.out.println(reverseWords(s));
    }
}
